(function(){
    $(document).ready(function(){
       function App(){};
        App.prototype.ajaxFn=function(){
            _$.post('/getautoPlay',function(data){
                //console.log(data);
                var info=data['info'];
                if(data['code']==1){
                    if(info.length>0){
                        for(var val in info){
                            var html = template('autoplaytpl', info[val]);
                            $("#lb ul").append(html);
                        }
                    }
                    //轮播图初始化
                    var slider = new fz.Scroll('.ui-slider', {
                        role: 'slider',
                        indicator: true,
                        autoplay: true,
                        interval: 3000
                    });
                }
            });
           _$.post('/getrmxsBook',function(data){
               //console.log(data);
               var info=data['info'];
               if(data['code']==1){
                   if(info.length>0){
                       for(var val in info){
                           info[val]['index']=parseInt(val)+1;
                           var html = template('rmxstpl', info[val]);
                           $("#rmxs ul").append(html);
                       }
                   }
               }
           });
           _$.post('/getxsmfBook',function(data){
               console.log(data);
               var info=data['info'];
               if(data['code']==1){
                   if(info.length>0){
                       for(var val in info){
                           info[val]['index']=parseInt(val)+1;
                           if(info[val]['stateType']=="T"){
                               info[val]['stateType']="连载中";
                           }else if(info[val]['stateType']=="F"){
                               info[val]['stateType']="完结";
                           }
                           if(info[val]['cnt']>10000){
                               info[val]['cnt']=(info[val]['cnt']/10000).toFixed(2)+"万字";
                           }else{
                               info[val]['cnt']=info[val]['cnt']+"字";
                           }
                           var html = template('xsmftpl', info[val]);
                           $("#xsmf").append(html);
                       }
                   }
               }
           });
           _$.post('/getrxbBook',function(data){
               //console.log(data);
               var info=data['info'];
               if(data['code']==1){
                   if(info.length>0){
                       for(var val in info){
                           info[val]['index']=parseInt(val)+1;
                           var html = template('rxbtpl', info[val]);
                           $("#rxb ul").append(html);
                       }
                   }
               }
           });
           _$.post('/getfybBook',function(data){
               //console.log(data);
               var info=data['info'];
               if(data['code']==1){
                   if(info.length>0){
                       for(var val in info){
                           info[val]['index']=parseInt(val)+1;
                           var html = template('rxbtpl', info[val]);
                           $("#fyb ul").append(html);
                       }
                   }
               }
           });
           _$.post('/getypbBook',function(data){
               //console.log(data);
               var info=data['info'];
               if(data['code']==1){
                   if(info.length>0){
                       for(var val in info){
                           info[val]['index']=parseInt(val)+1;
                           var html = template('rmxstpl', info[val]);
                           $("#ypb ul").append(html);
                       }
                   }
               }
           });
           _$.post('/gettjbBook',function(data){
               //console.log(data);
               var info=data['info'];
               if(data['code']==1){
                   if(info.length>0){
                       for(var val in info){
                           info[val]['index']=parseInt(val)+1;
                           var html = template('rxbtpl', info[val]);
                           $("#tjb ul").append(html);
                       }
                   }
               }
           });
           _$.post('/getxsqxBook',function(data){
               //console.log(data);
               var info=data['info'];
               if(data['code']==1){
                   if(info.length>0){
                       for(var val in info){
                           info[val]['index']=parseInt(val)+1;
                           if(info[val]['stateType']=="T"){
                               info[val]['stateType']="连载中";
                           }else if(info[val]['stateType']=="F"){
                               info[val]['stateType']="完结";
                           }
                           if(info[val]['cnt']>10000){
                               info[val]['cnt']=(info[val]['cnt']/10000).toFixed(2)+"万字";
                           }else{
                               info[val]['cnt']=info[val]['cnt']+"字";
                           }
                           var html = template('xsqxtpl', info[val]);
                           $("#xsqx").append(html);
                       }
                   }
               }
           });
           _$.post('/getqxwbBook',function(data){
               //console.log(data);
               var info=data['info'];
               if(data['code']==1){
                   if(info.length>0){
                       for(var val in info){
                           info[val]['index']=parseInt(val)+1;
                           if(info[val]['stateType']=="T"){
                               info[val]['stateType']="连载中";
                           }else if(info[val]['stateType']=="F"){
                               info[val]['stateType']="完结";
                           }
                           if(info[val]['cnt']>10000){
                               info[val]['cnt']=(info[val]['cnt']/10000).toFixed(2)+"万字";
                           }else{
                               info[val]['cnt']=info[val]['cnt']+"字";
                           }
                           var html = template('xsqxtpl', info[val]);
                           $("#qxwb").append(html);
                       }
                   }
               }
           });
           _$.post('/getxhqhBook',function(data){
               //console.log(data);
               var info=data['info'];
               if(data['code']==1){
                   if(info.length>0){
                       for(var val in info){
                           info[val]['index']=parseInt(val)+1;
                           var html = template('rxbtpl', info[val]);
                           $("#xhqh ul").append(html);
                       }
                   }
               }
           });
           _$.post('/getxxwxBook',function(data){
               //console.log(data);
               var info=data['info'];
               if(data['code']==1){
                   if(info.length>0){
                       for(var val in info){
                           info[val]['index']=parseInt(val)+1;
                           var html = template('rxbtpl', info[val]);
                           $("#xxwx ul").append(html);
                       }
                   }
               }
           });
           _$.post('/getdszcBook',function(data){
               //console.log(data);
               var info=data['info'];
               if(data['code']==1){
                   if(info.length>0){
                       for(var val in info){
                           info[val]['index']=parseInt(val)+1;
                           var html = template('rxbtpl', info[val]);
                           $("#dszc ul").append(html);
                       }
                   }
               }
           });
           _$.post('/getlsjsBook',function(data){
               //console.log(data);
               var info=data['info'];
               if(data['code']==1){
                   if(info.length>0){
                       for(var val in info){
                           info[val]['index']=parseInt(val)+1;
                           var html = template('rxbtpl', info[val]);
                           $("#lsjs ul").append(html);
                       }
                   }
               }
           });
           _$.post('/getyxtyBook',function(data){
               //console.log(data);
               var info=data['info'];
               if(data['code']==1){
                   if(info.length>0){
                       for(var val in info){
                           info[val]['index']=parseInt(val)+1;
                           var html = template('rxbtpl', info[val]);
                           $("#yxty ul").append(html);
                       }
                   }
               }
           });
           _$.post('/getkhlyBook',function(data){
               //console.log(data);
               var info=data['info'];
               if(data['code']==1){
                   if(info.length>0){
                       for(var val in info){
                           info[val]['index']=parseInt(val)+1;
                           var html = template('rxbtpl', info[val]);
                           $("#khly ul").append(html);
                       }
                   }
               }
           });
           _$.post('/getecyBook',function(data){
               //console.log(data);
               var info=data['info'];
               if(data['code']==1){
                   if(info.length>0){
                       for(var val in info){
                           info[val]['index']=parseInt(val)+1;
                           var html = template('rmxstpl', info[val]);
                           $("#ecy ul").append(html);
                       }
                   }
               }
           });
       };
        App.prototype.eventFn=function(){
           $("#searchGuide").click(function(){
                $("#searchPopup").show();
                $("#keyword").focus();
           });
           var localRead=localStorage.getItem('OLD_READ');
           if(localRead==""||localRead==null||localRead==undefined){
               $("#recentlyRead ol").html();
               $(".book-li-more").html("暂无阅读记录");
           }else{
               var localReadObj=JSON.parse(localRead);
               if(localReadObj.length>0){
                   for(var val in localReadObj){
                       localReadObj[val]['time']=getTimes(localReadObj[val]['time']);
                       var html = template('tpl', localReadObj[val]);
                       $("#recentlyRead ol").append(html);
                   }
               }
               $(".book-li-more").html("以上为全部阅读记录");
           }
           if(location.hash=='#&aside'){
               _$("#aside").animate({width:'100%'},100);
               $("html").css('overflow','hidden');
           }
           $("#asideTrigger").click(function(){
               _$("#aside").animate({width:'100%'},100);
               $("html").css('overflow','hidden');
           });
           $(document).swipeRight(function(){
               _$("#aside").animate({width:'0'},100);
               $("html").css('overflow','auto');
               location.hash="";
           });
           $("#asideOverlay").click(function(){
               _$("#aside").animate({width:'0'},100);
               $("html").css('overflow','auto');
               location.hash="";
           });
           //广告加载完毕
           _$("img.lazy1").lazyload({effect: "fadeIn"});
           function tabClick(dom,dom1){
               $(dom).on('click',function(){
                   $(this).children('.btn-tab').addClass('active');
                   $(this).siblings('h3').children('.btn-tab').removeClass('active');
                   $(dom1+" .ui-scroller").eq($(this).index()).show().siblings('.ui-scroller').hide();
               });
           }
           tabClick("#phb h3","#phbContent");
           tabClick("#fltj h3","#fltjContent");
           tabClick("#fltjq h3","#fltjqContent");
           $(window).scroll(function() {
               var top = $("body").scrollTop();
               var footerTop=$(".footer").offset().top-540;
               if(top>=500&&top<=footerTop){
                    $(".jsBackToTop").show();
               }else{
                   $(".jsBackToTop").hide();
               }
               if(top>=footerTop){
                    $("#asideTrigger").hide();
               }else{
                   $("#asideTrigger").show();
               }
           });
       };
        App.prototype.init=function(){
           //事件调用
           this.ajaxFn();
           this.eventFn();
       };
       //c程序初始化
       new App().init();
    })
})();

